/*
 * These files are distributed under the terms of the GNU GPLv2, the GNU General
 * Public version 2. See LICENSE.txt
 */
package fileCommodore.plugin.externalEditor;

import java.awt.event.*;
import java.io.*;

import fileCommodore.*;
import fileCommodore.model.*;
import fileCommodore.plugin.*;

/**
 * @author nado18@gmail.com, on Aug 1, 2007
 */
public class ExternalEditorPlugin extends Plugin
{
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see fileCommodore.plugin.Plugin#activeSelectionChanged()
	 */
	@Override
	public void activeSelectionChanged()
	{
		// TODO Auto-generated method stub
		
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see fileCommodore.plugin.Plugin#handle(java.awt.event.KeyEvent)
	 */
	@Override
	public void handle( KeyEvent ev )
	{
		if (KeyEvent.VK_F4 == ev.getKeyCode())
		{
			FileRowItem current = getActiveSelection().getSelected();
			FileRowItem parent = getActiveSelection().getCurrent();
			if (null != current && null != parent)
			{
				try
				{
					File workingDirectory = toFile( parent.getFileObject() );
					String itemPath =
							toFile( current.getFileObject() ).getAbsolutePath();
					
					String[] command = new String[]
						{
							Config.getExternalTextEditorCommand(), itemPath
						};
					
					launchProcessWithoutTerminal( workingDirectory, command );
				}
				catch (Exception e)
				{
					showError(
						"External Editor Error",
						"Could not open the external editor on the selected item.",
						e );
				}
			}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see fileCommodore.plugin.Plugin#inactiveSelectionChanged()
	 */
	@Override
	public void inactiveSelectionChanged()
	{
		// TODO Auto-generated method stub
		
	}
	
}
